package org.springframework.context.support;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;

/**
 * 实现了refresh刷新容器的基础方法的抽象容器
 */
public abstract class AbstractRefreshableApplicationContext extends AbstractApplicationContext {
    private DefaultListableBeanFactory beanFactory;

    /**
     * 创建bean工厂，并且加载bean定义对象
     *
     * @throws BeansException
     */
    @Override
    protected final void refreshBeanFactory() throws BeansException {
        //创建bean工厂
        DefaultListableBeanFactory beanFactory = createBeanFactory();
        //加载bean定义对象
        loadBeanDefinitions(beanFactory);

        this.beanFactory = beanFactory;
    }


    /**
     * 实现了创建一个空的bean工厂
     *
     * @return
     */
    protected DefaultListableBeanFactory createBeanFactory() {
        return new DefaultListableBeanFactory();
    }

    /**
     * 加载bean定义对象到工厂里
     * @param beanFactory
     * @throws BeansException
     */
    protected abstract void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws BeansException;


    @Override
    public DefaultListableBeanFactory getBeanFactory() {
        return beanFactory;
    }
}
